package com.example.myapplication.flow

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.example.myapplication.R
import com.example.myapplication.databinding.FlowActivityBinding
import com.orhanobut.logger.Logger
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/8/29$ 10:34 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/29$ 10:34 AM$
 * 修改备注:  [说明本次修改内容]
 */
@AndroidEntryPoint
class FlowActivity2 : AppCompatActivity(), CoroutineScope by MainScope() {
    lateinit var binding: FlowActivityBinding
    val flowViewModel: FlowViewModel by viewModels()
    val viewModle2: FlowViewModel2 by viewModels()

    val flowViewModel0908: FlowViewModel0908 by viewModels()
    val flowViewModel0910: FlowViewModel0910 by viewModels()
    val flowViewModel0912: FlowViewModel0912 by viewModels()
    val flowLIvedata0913: FlowLIvedata0913 by viewModels()
    val flowLIvedata0923: Flowviewmodel0923 by viewModels()

    @Inject
    lateinit var bean: Bean


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<FlowActivityBinding>(this, R.layout.flow_activity)
//        a1()
//        a2()
//        flowViewModel.a3()
//        flowViewModel.login()
//        oberver()
//        viewModle2.t1()
        launch {
//            viewModle2.operator2()
//            flowViewModel0908.getart()
//            flowViewModel0908.get0909()
//            flowViewModel0908.zip0909()
            flowLIvedata0923.flow1()

        }

        var job = flowLIvedata0923.countDownCoroutines(60, lifecycleScope,
            onTick = {

            }, onStart = {

            }, onFinish = {

            })

//        flowViewModel0910.t1()
//        flowViewModel0912.map()
//        flowViewModel0912.zip()
//        flowViewModel0910.reliu()
//        flowViewModel0908.getart("1")
        flowLIvedata0913.foo.observe(this, {
            Logger.d("FlowActivity2===onCreate: " + it);

        })
        launch {
            flowLIvedata0913.uistate2.collect {
                Logger.d("FlowActivity2===onCreate:collect== " + it);
            }
            flowLIvedata0913.uistate2.map {
                !it
            }.collect {
                Logger.d("FlowActivity2===onCreate: map=" + it);

            }


        }
        val uijob = lifecycleScope.launch {
            flowLIvedata0913.pui2.collect {

            }
        }

        Logger.d("FlowActivity2===onCreate: sss=" + bean.age);
        Logger.d("===: ");

    }

    private fun oberver() {
        launch {
            flowViewModel.sharedFlow.collect {
                binding.textView9.text = binding.textView9.text.toString() + it
            }
        }
        flowViewModel.uilivedata.observe(this) {
            binding.textView9.text = binding.textView9.text.toString() + it
        }
    }


    private fun a2() {
        launch {
            flow {
                emit(1)
                kotlinx.coroutines.delay(100)
                emit(2)
            }.transform { value ->
                if (value == 1) {
                    emit(value.toString() + "it")
                }
                emit(value.toString() + "不转换")
            }.collect {
                binding.textView9.text = binding.textView9.text.toString() + it
                Logger.d("FlowActivity2===a2: " + it);

            }
        }

    }

    private fun a1() {
        val huidao = flow {
            emit(1)
        }.onStart {
            emit(2)
        }.onCompletion {
            emit(3)
        }.map { value ->
            value * 2
        }
            .onEach {
//                    binding.textView9.text = binding.textView9.text.toString() + it
            }

        launch {
            huidao.collect {
                binding.textView9.text = binding.textView9.text.toString() + it
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }
}